﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ScrumTime.Models;

namespace ScrumTime.Services
{
    public class AcceptanceCriteriaService
    {
        ScrumTimeEntities _ScrumTimeEntities;

        public AcceptanceCriteriaService(ScrumTimeEntities scrumTimeEntities)
        {
            _ScrumTimeEntities = scrumTimeEntities;
        }


        public static AcceptanceCriteria GetAcceptanceCriteriaById(ScrumTimeEntities scrumTimeEntities, int id)
        {
            AcceptanceCriteria acceptanceCriteria = null;
            var results = from t in scrumTimeEntities.AcceptanceCriterias
                          where t.AcceptanceCriteriaId == id
                          select t;
            if (results.Count() > 0)
                acceptanceCriteria = results.First<AcceptanceCriteria>();
            else
                acceptanceCriteria = new AcceptanceCriteria();
            return acceptanceCriteria;
        }

        public AcceptanceCriteria GetAcceptanceCriteriaById(int id)
        {
            return GetAcceptanceCriteriaById(_ScrumTimeEntities, id);
        }

        public static List<AcceptanceCriteria> GetAcceptanceCriteriasByStoryIdOrderbyIdAsc(ScrumTimeEntities scrumTimeEntities, int storyId)
        {
            var results = from t in scrumTimeEntities.AcceptanceCriterias
                          where t.StoryId == storyId
                          orderby t.AcceptanceCriteriaId ascending
                          select t;            
            return results.ToList<AcceptanceCriteria>();
        }

        public List<AcceptanceCriteria> GetAcceptanceCriteriasByStoryIdOrderbyIdAsc(int storyId)
        {
            return GetAcceptanceCriteriasByStoryIdOrderbyIdAsc(_ScrumTimeEntities, storyId);
        }

        public AcceptanceCriteria SaveAcceptanceCriteria(AcceptanceCriteria acceptanceCriteria)
        {
            if (acceptanceCriteria != null)
            {
                if (acceptanceCriteria.AcceptanceCriteriaId == 0)  // this is new
                {
                    _ScrumTimeEntities.AddToAcceptanceCriterias(acceptanceCriteria);
                }
                else  // the story exists
                {
                    _ScrumTimeEntities.AttachTo("AcceptanceCriterias", acceptanceCriteria);

                    ScrumTimeEntities freshScrumTimeEntities =
                        new ScrumTimeEntities(_ScrumTimeEntities.Connection.ConnectionString);
                    AcceptanceCriteria existingAcceptanceCriteria = GetAcceptanceCriteriaById(freshScrumTimeEntities, acceptanceCriteria.AcceptanceCriteriaId);
                    if (existingAcceptanceCriteria == null)
                    {
                        throw new Exception("The AcceptanceCriteria no longer exists.");
                    }
                    _ScrumTimeEntities.ObjectStateManager.ChangeObjectState(acceptanceCriteria, System.Data.EntityState.Modified);
                }
                _ScrumTimeEntities.SaveChanges();
            }
            return acceptanceCriteria;
        }

        public void DeleteAcceptanceCriteria(int acceptanceCriteriaId)
        {
            AcceptanceCriteria existingAcceptanceCriteria = GetAcceptanceCriteriaById(acceptanceCriteriaId);

            if (existingAcceptanceCriteria != null && existingAcceptanceCriteria.AcceptanceCriteriaId > 0)
            {
                CheckDependency(existingAcceptanceCriteria);
                _ScrumTimeEntities.DeleteObject(existingAcceptanceCriteria);
                _ScrumTimeEntities.SaveChanges();
            }
            else
                throw new Exception("You have attempted to delete a Acceptance Criteria that does not exist.");
        }

        private void CheckDependency(AcceptanceCriteria acceptanceCriteria)
        {
            //verificamos la dependencia con otras tablas

            //las dependencias con la tabla affectedAcceptanceCriteria se eliminan
            // direcatamente en cascada por base de datos
        }

    }
}